var express = require('express');
var router = express.Router();
var mysql = require('./tool/mysql');
var url = require('url');
var multer = require('multer');
var upload = multer({dest:"uploads/"});
var fs = require('fs');
/* GET users listing. */
router.get('/', function(req, res, next) {
  var bannerType = url.parse(req.url, true).query.bannerType * 1;
// res.send('respond with a resource');
  var bannerStr = "";
  switch (bannerType) {
      case 0:
          bannerStr = "首页"
          break;
      case 1:
          bannerStr = "分类"
  }
  mysql.connect(function(db){
    console.log("success-banner");
    var queryObj = {
        bannerType:bannerType
    };
    var showObj = {
        _id:0
    }
    mysql.find(db,"banner",queryObj,showObj,function(result){
        res.render('banner', {title: "精彩家庭-轮播图管理", activeIndex: 6, isBoss: true, bannerStr:bannerStr,bannerType:bannerType,result:result })
    })
  }) 
});

// 轮播图添加页面
router.get('/addbanner', function(req, res, next) {
  var bannerType = url.parse(req.url, true).query.bannerType * 1;
  // res.send('respond with a resource');
  var bannerStr = "";
  switch (bannerType) {
      case 0:
          bannerStr = "首页"
          break;
      case 1:
          bannerStr = "分类"
  }
  res.render('addbanner', {title: "精彩家庭-轮播图管理", activeIndex: 6, isBoss: true, bannerStr:bannerStr,bannerType:bannerType })
});
// 轮播图form表单提交
router.post('/addbannerAction', upload.single('bannerImg'), function(req, res, next) {
  var obj = req.body;//表单数据
  var bannerType = obj.bannerType*1;
  console.log(req.file)//文件信息
  /**
   * { fieldname: 'bannerImg',
originalname: '1.bmp',
encoding: '7bit',
mimetype: 'image/bmp',
destination: 'uploads/',
filename: '522d3640eda0dfa916fe4551903026e3',
path: '\\uploads\\522d3640eda0dfa916fe4551903026e3',
size: 2389736 }
   */
  var type = req.file.mimetype.split('/')[1];
  var bannerSrc = "http://localhost:3000/" + req.file.filename +"."+type;

  mysql.connect(function(db){
      console.log("success-----banner1")
      var insertData = {
          bannerType: bannerType,
          bannerID: obj.bannerID,
          bannerSrc: bannerSrc,
          linkSrc: obj.linkSrc
      }
      fs.rename("uploads/"+req.file.filename,"uploads/"+req.file.filename+"."+type,function(err,data) {
        if(err) {
            console.log(err)
        }else{
            console.log("ok----------")
            mysql.insert(db,'banner',insertData, function(result){
                console.log(result)
                // res.render('banner', {title: "锋影-轮播图管理", activeIndex: 6, isBoss: true, bannerStr:bannerStr,bannerType:bannerType })
                //重定向
                res.redirect("/banner?bannerType="+bannerType);
                db.close();
            })
        }
    })
  })
});

// 轮播图编辑 更新
router.get('/updateBanner', function(req, res, next) {
    var bannerID = url.parse(req.url,true).query.bannerID;
    mysql.connect(function(db){
      console.log("success-banner");
      console.log(bannerID)
      var querObj = {bannerID : bannerID};
      var showObj = {
        _id: 0,
      }
      mysql.find(db,"banner",querObj,showObj,function(result){
          obj ={
            title:"更新轮播图",
            activeIndex:6,
            isBoss: false,
            result: result
          }
          res.render("updateBanner",obj);
          console.log(result);
          db.close();
      })
    })
  });

//   轮播图删除
router.get('/deleteBanner',function(req, res, next){
    var bannerID = url.parse(req.url,true).query.id;
    var bannerType = url.parse(req.url,true).query.type - 0;
    mysql.connect(function(db){
      var deleteObj = {
        bannerID: bannerID,
        bannerType: bannerType
      };
      mysql.deleteOne(db,'banner',deleteObj,function(result){
        res.redirect("/banner?bannerType=" + bannerType);
        db.close();
      })
    })
  });

module.exports = router;
